<?php

namespace App\Models;


use App\Events\Out\EventGiftLessonGifted;
use App\Facades\KafkaBridge;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;

class GiftLessonAcceptance extends BaseModels
{
    use SoftDeletes;

    protected $table = 'gift_lesson_acceptances';
    protected $primaryKey = 'id';
    protected $dates = ['expired_at', 'gifted_at'];

    const STATUS_ACCEPTED = 0;
    const STATUS_PURCHASED = 1;
    const STATUS_EXPIRED = 2;
    const STATUS_REFUND = 3;

    const EXPIRE_DAYS = 30;

    public function share()
    {
        return $this->belongsTo(GiftLessonShare::class, 'share_id');
    }

    public function giftReference($billId, Carbon $paidAt)
    {
        $share = $this->share;
        if (empty($share)) return;
        if ($share->getStatus() != GiftLessonShare::STATUS_ACCEPTED) return;
        if ($paidAt->greaterThan($this->getExpiredAt())) {
            $this->setStatus(self::STATUS_EXPIRED);
            $this->save();
            return;
        }
        $this->setStatus(self::STATUS_PURCHASED);
        $this->setGiftedAt($paidAt);
        $this->setBillId($billId);
        $this->save();

        $share->setStatus(GiftLessonShare::STATUS_GIFTED);
        $share->setLessonCount(GiftLessonStudent::GIFT_LESSON_COUNT);
        $share->save();

        $student = $share->student;
        if (!empty($student)) {
            $student->increaseGift(GiftLessonStudent::GIFT_LESSON_COUNT);
            $student->save();
        }

        $event = new EventGiftLessonGifted();
        $event->studentId = $share->getStudentId();
        $event->fromStudentId = $this->getStudentId();
        $event->billId = $billId;
        $event->lessonCount = GiftLessonStudent::GIFT_LESSON_COUNT;
        $event->shareCode = $share->getShareCode();

        KafkaBridge::push($event);
    }

    public function getId()
    {
        return $this->id;
    }

    public function getStudentId()
    {
        return $this->student_id;
    }

    public function setStudentId($value)
    {
        $this->student_id = $value;
    }

    public function getShareId()
    {
        return $this->share_id;
    }

    public function setShareId($value)
    {
        $this->share_id = $value;
    }

    public function getLessonCount()
    {
        return $this->lesson_count;
    }

    public function setLessonCount($value)
    {
        $this->lesson_count = $value;
    }

    public function getExpiredAt()
    {
        return $this->expired_at;
    }

    public function setExpiredAt($value)
    {
        $this->expired_at = $value;
    }

    public function getBillId()
    {
        return $this->bill_id;
    }

    public function setBillId($value)
    {
        $this->bill_id = $value;
    }

    public function getGiftedAt()
    {
        return $this->gifted_at;
    }

    public function setGiftedAt($value)
    {
        $this->gifted_at = $value;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($value)
    {
        $this->status = $value;
    }
}